package com.huanuo.internetofthings.controller.impact;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.unicorn.framework.core.ResponseDto;
import org.unicorn.framework.core.SysCode;
import org.unicorn.framework.core.exception.PendingException;

import com.huanuo.internetofthings.code.ResCode;
import com.huanuo.internetofthings.service.impact.facade.ImpactDeviceStatisticsFacadeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping({ "/impact/statistics" })
@Api(value = "IMPACT统计模块", tags = { "IMPACT统计模块" })
public class ImpactStatisticsController {

	@Autowired
	private ImpactDeviceStatisticsFacadeService impactDeviceStatisticsFacadeService;
    /**
     * 统计用户所在组按设备类型的汇总情况    返回 设备总数、在网总数情况
     * @return
     * @throws PendingException
     */
	@ApiOperation("统计用户所在组按设备类型的汇总情况    返回 设备总数、在网总数情况")
	@RequestMapping(value = { "/deviceType" }, method = { RequestMethod.GET })
	public ResponseDto<?> statisticsByDeviceType() throws PendingException {
		String userIdentity = (String) SecurityUtils.getSubject().getPrincipal();
		if (StringUtils.isBlank(userIdentity)) {
			throw new PendingException(SysCode.SESSION_ERROR);
		}
		return impactDeviceStatisticsFacadeService.statisticsByDeviceType(userIdentity);
	}
	
	/**
     * 统计用户所在组  设备总数，在网总数、设备运行总时长
     * @return
     * @throws PendingException
     */
	@ApiOperation("统计用户所在组  设备总数，在网总数、设备运行总时长")
	@RequestMapping(value = { "/group" }, method = { RequestMethod.GET })
	public ResponseDto<?> statisticsByGroup() throws PendingException {
		String userIdentity = (String) SecurityUtils.getSubject().getPrincipal();
		if (StringUtils.isBlank(userIdentity)) {
			throw new PendingException(SysCode.SESSION_ERROR);
		}
		return impactDeviceStatisticsFacadeService.statisticsByGroupNo(userIdentity);
	}
	
	/**
     * 统计用户所在组  设备开工时长月度分布情况
     * @return
     * @throws PendingException
     */
	@ApiOperation("统计用户所在组  设备开工时长月度分布情况")
	@RequestMapping(value = { "/startingTime/distribution" }, method = { RequestMethod.GET })
	public ResponseDto<?> startingTimeDistribution() throws PendingException {
		String userIdentity = (String) SecurityUtils.getSubject().getPrincipal();
		if (StringUtils.isBlank(userIdentity)) {
			throw new PendingException(SysCode.SESSION_ERROR);
		}
		return impactDeviceStatisticsFacadeService.startingTimeDistribution(userIdentity);
	}
	
	/**
     * 统计设备开工时长环比增长率分布
     * @return
     * @throws PendingException
     */
	@ApiOperation("统计设备开工时长环比增长率分布")
	@RequestMapping(value = { "/startingTime/ringRate/distribution" }, method = { RequestMethod.GET })
	public ResponseDto<?> startingTimeRingRateDistribution() throws PendingException {
		String userIdentity = (String) SecurityUtils.getSubject().getPrincipal();
		if (StringUtils.isBlank(userIdentity)) {
			throw new PendingException(SysCode.SESSION_ERROR);
		}
		return impactDeviceStatisticsFacadeService.startingTimeRingRateDistribution(userIdentity);
	}
	
	/**
     * 统计设备开工时长环比增长率分布
     * @return
     * @throws PendingException
     */
	@ApiOperation("统计设备开工时长环比增长率分布")
	@RequestMapping(value = { "/startingTime/yearOnYear/distribution" }, method = { RequestMethod.GET })
	public ResponseDto<?> startingTimeYearOnYearRatioDistribution() throws PendingException {
		String userIdentity = (String) SecurityUtils.getSubject().getPrincipal();
		if (StringUtils.isBlank(userIdentity)) {
			throw new PendingException(SysCode.SESSION_ERROR);
		}
		return impactDeviceStatisticsFacadeService.startingTimeYearOnYearRatioDistribution(userIdentity);
	}

	
	/**
     * 统计用户所在组  设备开工时长月度分布情况
     * @return
     * @throws PendingException
     */
	@ApiOperation("统计用户所在组  设备开工时长月度分布情况")
	@RequestMapping(value = { "/commencement/distribution" }, method = { RequestMethod.GET })
	public ResponseDto<?> commencementRateDistribution() throws PendingException {
		String userIdentity = (String) SecurityUtils.getSubject().getPrincipal();
		if (StringUtils.isBlank(userIdentity)) {
			throw new PendingException(SysCode.SESSION_ERROR);
		}
		return impactDeviceStatisticsFacadeService.commencementRateDistribution(userIdentity);
	}
	
	/**
     * 统计设备开工率环比增长分布情况
     * @return
     * @throws PendingException
     */
	@ApiOperation("统计设备开工率环比增长分布情况")
	@RequestMapping(value = { "/commencement/ringRate/distribution" }, method = { RequestMethod.GET })
	public ResponseDto<?> commencementRingRateDistribution() throws PendingException {
		String userIdentity = (String) SecurityUtils.getSubject().getPrincipal();
		if (StringUtils.isBlank(userIdentity)) {
			throw new PendingException(SysCode.SESSION_ERROR);
		}
		return impactDeviceStatisticsFacadeService.commencementRingRateDistribution(userIdentity);
	}
	
	/**
     * 统计设备开工率同比比增长分布情况
     * @return
     * @throws PendingException
     */
	@ApiOperation("统计设备开工率同比比增长分布情况")
	@RequestMapping(value = { "/commencement/yearOnYear/distribution" }, method = { RequestMethod.GET })
	public ResponseDto<?> commencementYearOnYearRateDistribution() throws PendingException {
		String userIdentity = (String) SecurityUtils.getSubject().getPrincipal();
		if (StringUtils.isBlank(userIdentity)) {
			throw new PendingException(SysCode.SESSION_ERROR);
		}
		return impactDeviceStatisticsFacadeService.commencementYearOnYearRateDistribution(userIdentity);
	}
	
}